import Vue from 'vue'
import Router from 'vue-router'
import { checkRouterAccess } from '../access/check'
import * as nodes from '../access/node'
Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '/login',
      name: 'login',
      meta: {
        title: '登录'
      },
      component: () => import('@/pages/login/Login')
    },
    {
      path: '/403',
      name: '403',
      meta: {
        title: '无权限'
      },
      component: () => import('@/pages/403')
    },
    {
      path: '/',
      meta: {
        title: '首页'
      },
      redirect: '/driveCabin',
      component: () => import('@/pages/Layout'),
      children: [
        {
          path: 'driveCabin',
          name: 'DriveCabin',
          meta: {
            title: '实时数据'
          },
          component: () => import('@/pages/DriveCabin/DriveCabin')
        }
      ]
    },
    {
      path: '/approval',
      redirect: '/approval/DefaultApproval',
      meta: {
        title: '审批'
      },
      component: () => import('@/pages/Layout'),
      children: [
        {
          path: 'DefaultApproval/:formType',
          name: 'DefaultApproval',
          meta: {
            title: '建设方案审批'
          },
          component: () => import('@/pages/approval/DefaultApproval')
        },
        {
          path: 'AFApproval/:formType',
          name: 'AFApproval',
          meta: {
            title: '安防工程验收'
          },
          component: () => import('@/pages/approval/DefaultApproval')
        },
        {
          path: 'jsfasp',
          name: 'jsfasp',
          meta: {
            title: '添加建设方案审批'
          },
          component: () => import('@/pages/approval/AddDefaultForm.vue')
        },
        {
          path: 'jsfasp/:id',
          name: 'jsfasp',
          meta: {
            title: '编辑建设方案审批'
          },
          component: () => import('@/pages/approval/AddDefaultForm.vue')
        },
        {
          path: 'afgcys',
          name: 'afgcys',
          meta: {
            title: '添加安防工程验收'
          },
          component: () => import('@/pages/approval/AddDefaultForm.vue')
        },
        {
          path: 'afgcys/:id',
          name: 'afgcys',
          meta: {
            title: '编辑安防工程验收'
          },
          component: () => import('@/pages/approval/AddDefaultForm.vue')
        },
        {
          path: 'Progress',
          name: 'Progress',
          meta: {
            title: '审批进度'
          },
          component: () => import('@/pages/approval/Progress')
        },
        {
          path: 'Progressdetail/:id',
          name: 'Progressdetail',
          meta: {
            title: '审批详情'
          },
          component: () => import('@/pages/approval/Progressdetail')
        },
        {
          path: 'FlowList',
          name: 'FlowList',
          meta: {
            title: '流程列表'
          },
          component: () => import('@/pages/approval/FlowList')
        },
        {
          path: 'AddFlow',
          name: 'AddFlow',
          meta: {
            title: '添加流程'
          },
          component: () => import('@/pages/approval/AddFlow')
        },
        {
          path: 'EditFlow/:id',
          name: 'EditFlow',
          meta: {
            title: '编辑流程'
          },
          component: () => import('@/pages/approval/AddFlow')
        },
        {
          path: 'TemplateList',
          name: 'TemplateList',
          meta: {
            title: '流程摸板列表'
          },
          component: () => import('@/pages/approval/TemplateList')
        },
        {
          path: 'AddTemplate',
          name: 'AddTemplate',
          meta: {
            title: '添加流程摸板'
          },
          component: () => import('@/pages/approval/AddTemplate')
        },
        {
          path: 'EditTemplate/:id',
          name: 'EditFlow',
          meta: {
            title: '编辑流程摸板'
          },
          component: () => import('@/pages/approval/AddTemplate')
        }
      ]
    },
    {
      path: '/base',
      redirect: '/base/role',
      meta: {
        title: '基础资料'
      },
      component: () => import('@/pages/Layout'),
      children: [
        {
          path: 'role',
          name: 'role',
          meta: {
            title: '角色列表'
          },
          component: () => import('@/pages/base/role/RoleList')
        },
        {
          path: 'role/edit/:id',
          name: 'roleId',
          meta: {
            title: '角色详情'
          },
          component: () => import('@/pages/base/role/RoleDetail')
        },
        {
          path: 'role/add',
          name: 'roleAdd',
          meta: {
            title: '角色添加'
          },
          component: () => import('@/pages/base/role/RoleDetail')
        },
        // 老的管理员列表 7月底发版先用这个
        {
          path: 'oldStaffList',
          name: 'oldStaffList',
          meta: {
            title: '管理员列表'
          },
          component: () => import('@/pages/base/staff/OldStaffList')
        }
      ]
    }
  ]
})

export default vue => {
  router.beforeEach(async (to, from, next) => {
    if (!sessionStorage.getItem('user_info')) {
      if (to.path === '/login') {
        next()
      } else {
        next({
          path: '/login',
          replace: true
        })
      }
    } else {
      if (to.path === '/login') {
        next({
          path: '/',
          replace: true
        })
      } else {
        next()
        // 判断权限
        if (checkRouterAccess(to)) {
          next()
        } else {
          next({
            path: '/403',
            replace: true
          })
        }
      }
    }
  })
  return router
}
